package com.signway.module_signway_adapter_Api;

import android.content.Context;
import android.os.signwaymanager.SignwayManager;
import android.util.Log;

import com.signway.open.api.IOpenAuthorizedApi;


/*
 * -----------------------------------------------------------------
 * Copyright (C) 2011-2019, by your Signway, All rights reserved.
 * -----------------------------------------------------------------
 *
 * ProjectName: droid5119
 *
 * Author: Administrator
 *
 * Email: pf.wang@Signway.cn
 *
 * Description:
 *
 * -----------------------------------------------------------------
 * 2019-8-31 : Create T960xAuthorizedApi.java
 * -----------------------------------------------------------------
 */
public class SignwayAuthorizedApi implements IOpenAuthorizedApi {
    private static final String TAG = "SignwayAuthorizedApi";
    private static SignwayAuthorizedApi mSignwayAuthorizedApi;
    private static Context mContext;

    private static final int AUTH_TYPE_ACTIVE = 1;         //激活类型3bytes
    private static final int AUTH_TYPE_DATE = 2;             //累计播放时长8
    private static final int AUTH_TYPE_TOTAL_TIME = 3;         //授权总时长8
    private static final int AUTH_TYPE_END_DATE = 4;           //授权结束日期10
    private static final int AUTH_TYPE_START_DATE = 5;         //授权开始日期10
    private final int dm2016AuthPos = 10;          //激活类型占了3位，起始位置：第10位
    private final int dm2016AuthDatePos = 13;      //累计播放时长占了8位，起始位置：第13位
    private final int dm2016AuthEndTimePos = 21;   //授权总时长占了8位，起始位置：第21位
    private final int dm2016AuthEndDatePos = 29;   //授权结束日期占了10位，起始位置：第29位
    private final int dm2016AuthStartDatePos = 39;   //授权开始日期占了10位，起始位置：第39位
    private SignwayManager signwayManager;

    public static synchronized SignwayAuthorizedApi getInstance(Context context) {
        mContext = context;
        if (mSignwayAuthorizedApi == null) {
            mSignwayAuthorizedApi = new SignwayAuthorizedApi(context);
        }
        return mSignwayAuthorizedApi;
    }

    public SignwayAuthorizedApi(Context context) {
        this.mContext = context;
        signwayManager = SignwayManager.getInstance(mContext);
    }

    @Override
    public int SystemAuthorizationWay() {
        int way = 0;
        return way;
    }


    /**
     * 设置激活状态
     *
     * @param value
     */
    @Override
    public void setAuthType(int value) {
        String strAuth = value + "";
        int strLen = strAuth.length();
        if (strLen != 3) {
            for (int i = 0; i < 3 - strLen; i++) {
                strAuth = "0" + strAuth;
            }
        }
        byte[] buffer = strAuth.getBytes();
        for (int i = 0; i < buffer.length; i++) {
            Log.i(TAG, "bufff--write-" + buffer[i]);
        }
        int aaa = signwayManager.setApkAuthInformations(AUTH_TYPE_ACTIVE, buffer, 3);

    }


    /**
     * 获取激活状态
     *
     * @param
     */
    @Override
    public int getAuthType() {
        byte[] buffer = new byte[3];
        int len = 3;
        int auth = 0;
        int length = signwayManager.getApkAuthInformations(AUTH_TYPE_ACTIVE, buffer, 3);
        if (length != len) {
            Log.e(TAG, "read getAuth failed");
        }
        try {
            int off = 0;
            for (int i = 0; i < len; i++) {
                off = i;
                if (buffer[i] == -1) {
                    break;
                }
            }
            auth = Integer.parseInt(new String(buffer, 0, off + 1));
        } catch (Exception e) {
            Log.i(TAG, "get auth failed: " + e.toString());
            auth = 0;
        }

        if ((auth != 0x00) && (auth != 0xA0) && (auth != 0xAB)) {
            auth = 0x00;
        }
        return auth;
    }

    @Override
    public void setAuthTotalTime(byte[] buffer, int buf_len) {

    }


    /**
     * 设置授权总时长
     *
     * @param
     */
    @Override
    public void setAuthTotalTime(long value) {
        String strTime = value + "";
        int len = strTime.length();
        if (len != 8) {
            for (int i = 0; i < 8 - len; i++) {
                strTime = "0" + strTime;
            }
        }
        byte[] buffer = strTime.getBytes();
        signwayManager.setApkAuthInformations(AUTH_TYPE_TOTAL_TIME, buffer, 8);

    }


    /**
     * 获取授权总时长
     *
     * @param
     */
    @Override
    public long getAuthTotalTime() {
        byte[] buffer = new byte[8];
        return signwayManager.getApkAuthInformations(AUTH_TYPE_TOTAL_TIME, buffer, 8);
    }

    @Override
    public void setAuthEndDate(byte[] buffer, int buf_len) {

    }


    @Override
    public void setAuthEndDate(String date) {
        byte[] buffer = date.getBytes();

        signwayManager.setApkAuthInformations(AUTH_TYPE_END_DATE, buffer, 10);

    }

    //获取授权失效日期
    @Override
    public String getAuthEndDate() {
        byte[] buffer = new byte[10];
        signwayManager.getApkAuthInformations(AUTH_TYPE_END_DATE, buffer, 10);
        return "";
    }

    @Override
    public void setAuthStartDate(byte[] buffer, int buf_len) {

    }


    @Override
    public void setAuthStartDate(String date) {
        byte[] buffer = date.getBytes();
        setAuthStartDate(buffer, buffer.length);
    }

    // 获取授权失效日期
    @Override
    public String getAuthStartDate() {
        byte[] buffer = new byte[10];
        signwayManager.getApkAuthInformations(AUTH_TYPE_START_DATE, buffer, 10);
        return "";
    }

    @Override
    public void setWorkTime(byte[] buffer, int buf_len) {

    }


    @Override
    public void setWorkTime(long value) {
        String strTime = value + "";
        int len = strTime.length();
        if (len != 8) {
            for (int i = 0; i < 8 - len; i++) {
                strTime = "0" + strTime;
            }
        }
        byte[] buffer = strTime.getBytes();
        for (int i = 0;i < buffer.length;i++){
            Log.i(TAG, "set work time buffer=" + buffer[i]);
        }
        signwayManager.setApkAuthInformations(AUTH_TYPE_ACTIVE, buffer, 8);
        Log.i(TAG, "set work time =" + value);
    }

    //获取累计播放时长
    @Override
    public long getWorkTime() {
        byte[] buffer = new byte[8];
        int len = 8;
        int length = signwayManager.getApkAuthInformations(AUTH_TYPE_ACTIVE, buffer, 8);
        if (length != len) {
            Log.i(TAG, "read getWorkTime failed");
        }
        for (int i = 0;i < buffer.length;i++){
            Log.i(TAG, "get work time buffer 1 =" + buffer[i]);
        }
        long date = 0;
        int date1 = 0;
        try {
            int off = 0;
            for (int i = 0; i < len; i++) {
                off = i;
                if (buffer[i] == -1) {
                    break;
                }
            }

            for (int i = 0;i < buffer.length;i++){
                Log.i(TAG, "get work time buffer 2 =" + buffer[i]+"--------"+off);
            }
            date = Long.parseLong(new String(buffer, 0, off + 1));
            date1 = Integer.parseInt(new String(buffer, 0, off + 1));
        } catch (Exception e) {
            Log.i(TAG, "get work time failed: " + e.toString());
            date = 0;
        }
        Log.i(TAG, "get work time =" + date+"--"+date1);
        return date;
    }
}
